﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Web.Framework;
using Entity;
using Service;
using Core;
using Core.ZTree;
using Entity.Admin;
using System.Threading;

namespace Web.Areas.Admin.Controllers
{
    public partial class MenuController : BaseController
    {
        private MenuService menuService;

        public MenuController()
        {
            menuService = new MenuService(db);
        }

        //
        // GET: /Admin/Menu/

        public virtual ViewResult Index()
        {
            return View(db.Menus.ToList());
        }

        //
        // GET: /Admin/Menu/Edit/5

        public virtual ActionResult Edit(string id, string parentItemID)
        {
            Thread.Sleep(2000);
            ViewData["Menus"] = MenuService.ToDropDownList(null);
            Menu menu = db.Menus.Find(id);
            if (menu == null)
            {
                menu = new Menu();
                menu.ParentID = parentItemID;
                menu.Enable = true;
            }
            return View(menu);
        }

        //
        // POST: /Admin/Menu/Edit/5

        [HttpPost]
        public virtual ActionResult Edit(Menu menu)
        {
            ViewData["Menus"] = MenuService.ToDropDownList(null);
            if (ModelState.IsValid)
            {
                //db.Entry(menu).State = EntityState.Modified;
                //db.SaveChanges();
                try
                {
                    menuService.CreateOrSave(menu);
                }
                catch (Exception ex)
                {
                    this.AddViewMessage(ex);
                    //ModelState.AddModelError(ex.Message, ex);
                    return View();
                }
                //return RedirectToAction("Index");
                this.AddViewMessage("编辑成功");
                return new AjaxLoadResult(Url.Action("Index"));
            }
            return View(menu);
        }

        //
        // GET: /Admin/Menu/Delete/5

        public virtual ActionResult Delete(string id)
        {
            Menu menu = db.Menus.Find(id);
            return View(menu);
        }

        //
        // POST: /Admin/Menu/Delete/5

        [HttpPost, ActionName("Delete")]
        public virtual ActionResult DeleteConfirmed(string id)
        {
            //Menu menu = db.Menus.Find(id);
            //db.Menus.Remove(menu);
            //db.SaveChanges();
            menuService.Delete(id);
            return RedirectToAction("Index");
        }


        public virtual ActionResult MoveTo(string id, string targetId, TreeNodeMoveType type)
        {
            menuService.MoveTo(id, targetId, type);
            return Content("ok");
        }

        public virtual ActionResult LeftMenu(string menuId)
        {
            var menus = menuService.GetMenuByParentId(menuId);
            ViewBag.menuId = menuId;
            return View(menus);
        }

        public virtual ActionResult TopMenu()
        {
            var menus = menuService.GetMenuByParentId(null);
            return View(menus);
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}